﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Quartz;
using TjZheng.Redis;
using TjZheng.Framework;
using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Business;
using TjZheng.Base;

namespace TjZheng.SyncBBSRedis
{
    public class SyncBadWods : TjZhengJob
    {
        public override void JobExecute(global::Quartz.IJobExecutionContext context)
        {
            LoadDataFromData();
            return;
            var objRedisClient = RedisHelper.GetClient("tjzheng", 1);
            string defaultPath = AppDomain.CurrentDomain.BaseDirectory + "敏感词.txt";
            string filePath = ConfigHelper.GetValue("filePath", defaultPath);
            if (!File.Exists(filePath))
            {
                ConsoleHelper.WriteLineRed(string.Format("对不起，{0}不存在", filePath));
                return;
            }
            DbSetProcess objDbSetProcess = new DbSetProcess();
            string redisKey = ConfigHelper.GetValue("redisKey", string.Format("{0}_BBS_BadWords", objDbSetProcess.BBSName));
            //StringBuilder objStringBuilder = new StringBuilder();
            List<string> objList = new List<string>();
            using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
            {
                ConsoleHelper.WriteLineYellow(objDbSetProcess.BBSName + "敏感词库开始导入");
                string line;
                while ((line = sr.ReadLine()).IsNoNull())
                {
                    objList.Add(line);
                    //ConsoleHelper.WriteLineYellow(line);
                    if (objList.Count == 100)
                    {
                        objRedisClient.AddRangeToSet(redisKey, objList);
                        objList = new List<string>();
                    }
                    //objRedisClient.AddItemToSet(redisKey, line.Trim());
                    //objStringBuilder.AppendLine(line);
                }
                if (objList.IsNoNull())
                {
                    objRedisClient.AddRangeToSet(redisKey, objList);
                }
                ConsoleHelper.WriteLineYellow("敏感词库导入成功");
            }
            //ConsoleHelper.WriteLineYellow(objList.Count);
            objRedisClient.Dispose();

            ConsoleHelper.WriteLineYellow(objDbSetProcess.BBSName + "敏感词库导入成功");
        }

        void LoadDataFromData()
        {
            string defaultPath = AppDomain.CurrentDomain.BaseDirectory + "敏感词.txt";
            string filePath = ConfigHelper.GetValue("filePath", defaultPath);
            if (!File.Exists(filePath))
            {
                ConsoleHelper.WriteLineRed(string.Format("对不起，{0}不存在", filePath));
                return;
            }
            DbSetProcess objDbSetProcess = new DbSetProcess();
            BadWords objBadWords = null;
            BizBadWords objBizBadWords = new BizBadWords(objDbSetProcess.ConnectionStringKey, "");
            List<BadWords> objList = new List<BadWords>();
            using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
            {
                ConsoleHelper.WriteLineYellow(objDbSetProcess.BBSName + "敏感词库开始导入");
                string line;
                while ((line = sr.ReadLine()).IsNoNull())
                {
                    objBadWords = new BadWords { Word = line };
                    objBadWords.SetState(EntityStatus.Added);
                    objList.Add(objBadWords);
                    //ConsoleHelper.WriteLineYellow(line);
                    if (objList.Count == 20)
                    {
                        objBizBadWords.Batch(objList);
                        objList = new List<BadWords>();
                    }
                }
                if (objList.IsNoNull())
                {
                    objBizBadWords.Batch(objList);
                }
                ConsoleHelper.WriteLineYellow("敏感词库导入成功");
            }
        }
    }
}
